import Vue from 'vue'

import dayjs from 'dayjs'

function formateTime(time) {
  let date = new Date(time * 1000)
  let D = date.getDay()
  let m = date.getMonth() + 1
  let d = date.getDate()
  let arr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']

  return `${arr[D]} ${m}月${d}日`
}

function today(time) {
  let date = new Date(time * 1000)
  let D = date.getDay()
  let m = date.getMonth() + 1
  let d = date.getDate()
  let arr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  if (time === new Date(new Date().toLocaleDateString()).getTime() / 1000) {
    return `今天 ${m}月${d}日`
  } else if (time === new Date(new Date().toLocaleDateString()).getTime() / 1000 + 86400) {
    return `明天 ${m}月${d}日`
  } else if (time === new Date(new Date().toLocaleDateString()).getTime() / 1000 + 86400 * 2) {
    return `后天 ${m}月${d}日`
  } else {
    return `${arr[D]} ${m}月${d}日`
  }
}

function getSalePrice(value) {
  return ('￥' + value).substr(0, 3)
}
Vue.filter('getFormateTime', (value, mater = 'YYYY-MM-DD HH:mm:ss') => dayjs(value).format(mater))
Vue.filter('formateTime', formateTime)
Vue.filter('getSalePrice', getSalePrice)
Vue.filter('today', today)

export function VueDebounce(fnName, time) {
  let timeout = null
  return function(...arg) {
    if (timeout) {
      clearTimeout(timeout)
    }
    timeout = setTimeout(() => {
      this[fnName].apply(null, arg)
    }, time)
  }
}
